import Mock from 'mockjs';

Mock.setup({
    timeout: '500-800',
});

// 用户数据数组
const user = [
    { id: '1', username: 'admin', name:'测试', password: '12345678Aa', mailbox: '11223344@qq.com',
      createDate:'2024-1-15 12:14:02', lastLogin:'2024-1-16 12:14:02', ifused: true, role:'product'},
    { id: '2', username: 'lili', name:'测试', password: 'Aa11223344', mailbox: '12312344@qq.com', 
      createDate:'2023-1-15 12:14:02', lastLogin:'2023-1-16 12:14:02',ifused: false, role:'order'},
    { id: '3', username: 'ranran', name:'测试', password: 'Aa123123', mailbox: '12312366@qq.com', 
      createDate:'2022-1-15 12:14:02', lastLogin:'2022-1-16 12:14:02',ifused: false, role:'super'}
];

Mock.mock(/\/user\/list/, 'get', () => {
  console.log('/user/list接口拦截');
  return { code: 1, msg: '获取成功', result: user };
});

Mock.mock(/\/user\/add/, 'post', (options) => {
  console.log('options', options);
  const { body } = options;
  user.push(JSON.parse(body));

  return { code: 1, msg: '添加成功', result: user };
});

Mock.mock(/\/user\/edit/, 'post', (options) => {
  console.log('/user/edit接口拦截');
  const { body } = options;
  const index = user.findIndex((ele) => ele.id === body.id)+1;
  user.splice(index, 1, JSON.parse(body));

  return { code: 1, msg: '修改成功', result: user };
});

Mock.mock(RegExp(`/user/get.*`), 'get', (options) => {
  console.log('/user/get/:id接口拦截');
  const arr = options.url.split('/');
  const id = arr[arr.length - 1];
  const data = user.find((ele) => ele.id === id);
  return { code: 1, msg: '获取成功', result: data };
});

Mock.mock(RegExp(`/user/delete.*`), 'delete', (options) => {
  const arr = options.url.split('/');
  const id = arr[arr.length - 1];
  const index = user.findIndex((ele) => ele.id === id);
  user.splice(index, 1);
  return { code: 1, msg: '删除成功' };
});